/**
 * Created by xn on 2015/11/3.
 */


var FRoundedRectNode = cc.Sprite.extend({
    ctor: function( fillColor, rectSize, roundRadius, lineColor, lineWidth) {
        this._super()
        this.setAnchorPoint(0.5, 0.5)
        this.drawNode = cc.DrawNode.create()
        this.addChild(this.drawNode)

        this.setContentSize((typeof rectSize == "number" ? cc.size(rectSize, rectSize) : rectSize) || cc.size(100, 100))
        this.fillColor = fillColor || cc.color(0, 0, 0, 255)
        this.roundRadius = roundRadius || 0
        this.lineColor = lineColor || this.fillColor
        this.lineWidth = lineWidth || 0
    },

    onEnterTransitionDidFinish: function() {
        this._super()
        this.drawNodeUpdate()
    },

    setFillColor: function(fillColor) {
        if ( fillColor ) {
            this.fillColor = fillColor
            this.lineColor = this.fillColor
            this.drawNodeUpdate()
        }
    },

    drawNodeUpdate: function() {
        if ( !this.getParent() )
            return

        this.drawNode.clear()
        var size = this.getContentSize()
        if ( this.roundRadius > 0 ) {
            var vertices = new Array()
            var segments = 100
            var iSize = cc.size(size.width - 2 * this.roundRadius, size.height - 2 * this.roundRadius)
            for ( var i = 0, coef = cc.PI2 /segments; i < segments; i++ ) {
                var anglePos = cc.pMult(cc.pForAngle((segments - i) * coef), this.roundRadius)
                if ( anglePos.x > 0  )
                    anglePos.x += iSize.width
                if ( anglePos.y > 0 )
                    anglePos.y += iSize.height
                vertices.push( cc.pAdd(anglePos, cc.p(this.roundRadius, this.roundRadius)))
            }
        this.drawNode.drawPoly( vertices, this.fillColor, this.lineWidth, this.lineColor)
        }
        else {
            this.drawNode.drawRect( cc.p(0, 0), cc.pFromSize(size), this.fillColor, this.lineWidth, this.lineColor )
        }
    }
})